package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.dao.bean.ItemSet;
import com.qilu.pe.dao.bean.SetType;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.ActiviceCollector;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class SetListActivity extends Base2Activity {
    private RecyclerView rcv_list;

    private List<ItemSet> setList = new ArrayList<>();
    private SetListAdapter adapter;

    private SetType setType;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_set_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        rcv_list = findViewById(R.id.rcv_list);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            setType = (SetType) bundle.getSerializable("setType");
        }

        setHeadTitle("套餐列表");
        if (null != setType) {
            setHeadTitle(setType.getName());

            adapter = new SetListAdapter(this);
            LinearLayoutManager manager = new LinearLayoutManager(this);
            rcv_list.setLayoutManager(manager);
            rcv_list.setAdapter(adapter);

            hideProgress();
            reqSetList();
            showProgress();
        } else {
            finish();
        }

        setListeners();
    }

    private void setListeners() {
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                // id = bundle.getString("id");
                // type = bundle.getString("type"); // 1 项目 2 套餐
                Bundle bundle = new Bundle();
                bundle.putString("id", setList.get(position).getId());
                bundle.putString("type", "2");
                startActivity(ItemSetDetailActivity.class, bundle);
            }
        });
    }

    // 请求套餐列表
    private Disposable reqSetList() {
        return APIRetrofit.getDefault().reqSetList(setType.getId(), 1, 999999)
                .compose(RxUtil.<BaseResult2<List<ItemSet>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<ItemSet>>>() {
                    @Override
                    public void accept(BaseResult2<List<ItemSet>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {
                            setList = listBaseResult2.getData();
                            adapter.setDataList(setList);
                        } else {
                            ToastUtils.showShort(listBaseResult2.getMsg());
                        }
                        if (isDataEmpty(setList)) {
                            ToastUtils.showShort("暂无数据");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class SetListAdapter extends ListBaseAdapter<ItemSet> {

        public SetListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_search_item_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_price = holder.getView(R.id.tv_price);
            TextView tv_day = holder.getView(R.id.tv_day);
            TextView tv_sig = holder.getView(R.id.tv_sig);

            ItemSet set = mDataList.get(position);
            tv_name.setText(set.getName());
            tv_price.setText("¥" + set.getPrice());
            tv_day.setText(set.getDay() + "个工作日");
            tv_sig.setText(set.getSignificance());
        }
    }
}